// Copyright [2020] <unknown>
#include <bits/stdc++.h>
using namespace std;
// #define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
#define dbg(x...) { cerr << "\033[32;1m" << #x << " -> "; err(x); }
void err() { cerr << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cerr << a << ' '; err(x...); }
#else
#define dbg(...)
#endif

#define sz(x) ((int)x.size())

typedef long long LL;

const int N = 2e5+50;

int cnt[N][26];

int t, k, n;
char s[N];

int main(int argc, char const *argv[]) {
    // code
    scanf("%d", &t);
    for (int kk=0; kk<t; ++kk) {
        scanf("%d%d", &n, &k);
        int tot = (k+1)/2;
        for (int i=0; i<tot; ++i)
            memset(cnt[i], 0, sizeof(cnt[i]));
        scanf("%s", s);
        for (int i=0; i<n; ++i) {
            int id = i % k;
            id = min(id, k-1-id);
            ++cnt[id][s[i]-'a'];
        }
        int ans = 0;
        for (int i=0; i<tot; ++i) {
            int sum = 0, mx = 0;
            for (int j=0; j<26; ++j) {
                sum += cnt[i][j];
                mx = max(mx, cnt[i][j]);
            }
            ans += sum - mx;
        }
        printf("%d\n", ans);
    }
    return 0;
}
